package com.xjd.edu.common.enums;


import com.xjd.edu.common.component.ApplicationContextRegister;
import com.xjd.edu.common.config.BizConfig;
import org.springframework.util.Assert;

/**
 * redis key 前缀
 */
public enum RedisKeyEnum {

    /**
     * 管理员token
     */
    ADMIN_TOKEN,
    /**
     * 财务后台管理员token
     */
    DX_ADMIN_TOKEN,
    /**
     * 用户token
     */
    PLAY_TOKEN,
    /**
     * 用户token
     */
    PLAY_TOKEN_MAPPER,
    /**
     * 皮皮App登录Token键（也适用于H5）
     */
    GAME_APP_TOKEN,
    /**
     * 皮皮App登录用户键（也适用于H5）
     */
    GAME_APP_TOKEN_MAPPING,
    /**
     * 小程序的sessionKey
     */
    WX_SESSION_KEY,
    /**
     * 微信消息推送
     */
    WX_TEMPLATE_MSG,
    /**
     * 微信推送点击去重
     */
    BITSET_PUSH_MSG_HITS,
    /**
     * 表单验证token
     */
    GLOBAL_FORM_TOKEN,
    /**
     * 用户在线状态
     */
    USER_ONLINE_KEY,
    /**
     * 时间间隔
     */
    TIME_INTERVAL_KEY,

    /**
     * 登录验证码
     */
    SMS_VERIFY_CODE,


    /**
     * 用户默认昵称生成
     */
    USER_NICK_NAME,
    /**
     * 用户兑换开黑券
     */
    USER_DIAMOND_GIFT_TICKET,
    // ===========聊天室联赛begin===============
    /**
     * 房间赛事获胜者
     * type: hash
     * key: ROOM_LEAGUE_WINNER:{type}
     * hash_key: 1-冠军 2-亚军 3-季军
     * hash_value: roomNo
     * type: 1-房间 2-主播
     */
    ROOM_LEAGUE_WINNER,
    /**
     * 房间赛事榜单
     * type: zset
     * key: ROOM_LEAGUE_RANK:{type}:{stage} or ROOM_LEAGUE_RANK:{type}:{stage}:{date:yyyyMMdd}
     * value: roomNo
     * score: 火力值
     * type: 1-房间 2-主播
     * stage: 1-选拔赛 2-循环赛 3-决赛
     */
    ROOM_LEAGUE_RANK,
    /**
     * 房间晋级名单
     * type: set
     * key: ROOM_LEAGUE_PROMOTION:{type}:{stage}
     * value: roomNoList
     */
    ROOM_LEAGUE_PROMOTION,
    /**
     * 房间PK连胜次数
     * type: hash
     * key: ROOM_PK_STREAK_VICTORY_COUNT:{type}
     * hash_key: roomNo
     * hash_value: victory_count
     * type: 1-房间 2-主播
     */
    ROOM_PK_STREAK_VICTORY_COUNT,
    /**
     * 房间PK获胜次数
     * type: hash
     * key: ROOM_PK_VICTORY_COUNT:{type}
     * hash_key: roomNo
     * hash_value: victory_count
     * type: 1-房间 2-主播
     */
    ROOM_PK_VICTORY_COUNT,
    /**
     * 房间PK结果
     * type: hash
     * key: ROOM_PK_RESULT:{type}:{dateStr}
     * hash_key: roomNo
     * hash_value: 0-失败 1-胜利
     * type: 1-房间 2-主播
     */
    ROOM_PK_RESULT,
    /**
     * 房间PK记录
     * type: hash
     * key: ROOM_PK_RECORDS:{type}:{roomNo}
     * hash_key: dateStr
     * hash_value: -1-失败 0-平局 1-胜利
     */
    ROOM_PK_RECORDS,
    /**
     * 幕后神豪排行榜
     * type: zset
     * key: LEAGUE_RICHER_RANK or LEAGUE_RICHER_RANK:{userId}
     * value: userId or roomNo
     * score: 火力值
     * stage: 1-选拔赛 2-循环赛 3-决赛
     */
    LEAGUE_RICHER_RANK,
    /**
     * 应援团排行榜
     * type: zset
     * key: LEAGUE_ENDAN_RANK  or LEAGUE_ENDAN_RANK:{roomNo}
     * value: roomNo
     * score: 火力值
     * stage: 1-选拔赛 2-循环赛 3-决赛
     */
    LEAGUE_ENDAN_RANK,
    /**
     * 房间PK对阵分配
     * type: hash
     * key: ROOM_PK_LIST:{type}:{dateStr}
     * hash_key: roomNo
     * hash_value: 对手roomNo
     * type: 1-房间 2-主播
     * stage: 1-选拔赛 2-循环赛 3-决赛
     */
    ;

    // 缓存时间2分钟
    public static final int two_min_time = 120;

    public static final String SPLIT = "-";
    public static final String SPLIT2 = ":";

    /**
     * Generate Redis Key
     *
     * @param parts
     * @return
     */
    public String generateRedisKey(String... parts) {
        Assert.notEmpty(parts, "参数 parts 不能为空");
        return String.format("%s:%s:%s", this.getEvnPrefix(), this.name(), String.join(SPLIT2, parts));
    }

    public String generateKey(int id) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + id;
    }

    public String generateKey(int parentId, int id) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + parentId + SPLIT + id;
    }

    public String generateKey(String id) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + id;
    }

    public String generateKey(String parentId, String id) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + parentId + SPLIT + id;
    }

    public String generateKey(String parentId, Integer id) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + parentId + SPLIT + id;
    }

    public String generateKey(Integer id, String str) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + id + SPLIT + str;
    }

    public String generateKey() {
        return getEvnPrefix() + SPLIT + this.name();
    }

    public String generateDirKey(int id) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT2 + id;
    }

    public String generateDirKey(String dir) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT2 + dir;
    }

    public String generateDirKey(String... dir) {
        StringBuilder builder = new StringBuilder(getEvnPrefix() + SPLIT + this.name());
        for (String key : dir) {
            builder.append(SPLIT2).append(key);
        }
        return builder.toString();
    }

    public String generateDirKey(Object... dir) {
        StringBuilder builder = new StringBuilder(getEvnPrefix() + SPLIT + this.name());
        for (Object key : dir) {
            builder.append(SPLIT2).append(key.toString());
        }
        return builder.toString();
    }

    public String generateDirKey(String dir, String key) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT2 + dir + SPLIT2 + key;
    }

    public String generateCommonActivityDirKey(String bizKey, String dir) {
        return getEvnPrefix() + SPLIT + bizKey + SPLIT2 + dir;
    }

    /**
     * 获取环境定义的前缀
     *
     * @return
     */
    private String getEvnPrefix() {
        BizConfig config = ApplicationContextRegister.getBean("bizConfigProperties", BizConfig.class);
        return config.getEvn().getPrefix();
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String day) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + day;
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String day, int statTimeEnum) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + day + SPLIT + statTimeEnum;
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String begin, String end) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + begin + SPLIT + end;
    }

    /**
     * 获取渠道统计相关key
     *
     * @return
     */
    public String getPlatformStatKey(String bizKey, int platform, int os, String begin, String end, int statTimeEnum, Integer labelId) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + bizKey + SPLIT + platform + SPLIT + os + SPLIT + begin + SPLIT + end + SPLIT + statTimeEnum + SPLIT + labelId;
    }

    /**
     * 获取邀新活动统计相关key
     *
     * @return
     */
    public String getInviteStatKey(String bizKey, Integer activityId, String begin, String end, int statTimeEnum) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + bizKey + SPLIT + activityId + SPLIT + begin + SPLIT + end + SPLIT + statTimeEnum;
    }

    /**
     * @return
     */
    public String getFaqKey(Integer userId, Integer faqId, String date) {
        return getEvnPrefix() + SPLIT + this.name() + SPLIT + userId + SPLIT + faqId + SPLIT + date;
    }

    /**
     * @return
     */
    public String getUserDrawKey(String cashNo) {
        return getEvnPrefix() + SPLIT + "userDrawRedisKey" + this.name() + SPLIT + cashNo;
    }

    /**
     * @return
     */
    public String getGuildDrawKey(String cashNo) {
        return getEvnPrefix() + SPLIT + "guildDrawRedisKey" + this.name() + SPLIT + cashNo;
    }


    public String getKey(Integer id1, Integer id2, Integer id3) {
        return getEvnPrefix() + SPLIT2 + this.name() + SPLIT2 + id1 + SPLIT2 + id2 + SPLIT2 + id3;
    }

    public String getKey(Integer id1, Integer id2, String id3) {
        return getEvnPrefix() + SPLIT2 + this.name() + SPLIT2 + id1 + SPLIT2 + id2 + SPLIT2 + id3;
    }

    public String getKey(Integer id1, Integer id2, Integer id3, Integer id4) {
        return getEvnPrefix() + SPLIT2 + this.name() + SPLIT2 + id1 + SPLIT2 + id2 + SPLIT2 + id3 + SPLIT2 + id4;
    }

    public String getKey(Integer id1, Integer id2) {
        return getEvnPrefix() + SPLIT2 + this.name() + SPLIT2 + id1 + SPLIT2 + id2 + SPLIT2;
    }

    public String getKey(Integer id1, String id2) {
        return getEvnPrefix() + SPLIT2 + this.name() + SPLIT2 + id1 + SPLIT2 + id2;
    }

    public String getKey(Integer id1) {
        return getEvnPrefix() + SPLIT2 + this.name() + SPLIT2 + id1;
    }


}
